package net.openl10n.packaging;

import java.io.IOException;

import net.openl10n.opc4j.Package;
import net.openl10n.opc4j.PackageAccess;
import net.openl10n.opc4j.PackagePartName;
import net.openl10n.opc4j.PackageProperties;
import net.openl10n.opc4j.exceptions.InvalidFormatException;
import net.openl10n.opc4j.exceptions.OPC4JException;

public abstract class OpenLocPackage extends OpenLocPartContainer{

	private final Package pack;
	private final PartExtensionProvider partExtensionProvider;
	
	protected OpenLocPackage(Package pack, OpenLocPartFactoryManager partFactoryManager) {
		super(pack,partFactoryManager);
		this.pack = pack;
		this.partExtensionProvider = new PartExtensionProvider();
	}
	
	public PartExtensionProvider getPartExtensionProvider(){
		return partExtensionProvider;
	}
	
	public PackageAccess getPackageAccess(){
		return pack.getPackageAccess();
	}
	
	public PackageProperties getPackageProperties() throws InvalidFormatException{
		return pack.getPackageProperties();
	}
	public Package getPackage(){
		return pack;
	}
	
	public void deletePartRecursive(PackagePartName partName) {
		pack.deletePartRecursive(partName);
	}
	
	public void close() throws IOException, InvalidFormatException, OPC4JException{
		for(IdPartPair parts : getParts()){
			parts.getPart().onClose();
		}
		pack.close();
	}
	
	public void validate() throws InvalidFormatException{
		pack.validatePackage(getPackage());
	}
}
